/**
 * 播放列表相关 Mock 接口
 */

import Mock from 'mockjs'
import type { MockConfig } from '../server'

const playlistMocks: MockConfig[] = [
  // 获取播放列表详情
  {
    url: /\/api\/playlist\/(\d+)/,
    method: 'GET',
    response: (options: any) => {
      const id = options.url.match(/\/api\/playlist\/(\d+)/)[1]
      return {
        code: 200,
        msg: '获取成功',
        data: Mock.mock({
          id: +id,
          name: '@ctitle(4, 10)',
          description: '@csentence(20, 50)',
          coverUrl: "@image('400x400', '@color', '#FFF', 'Playlist')",
          'playCount|10000-1000000': 1,
          'trackCount|20-100': 20,
          'subscriberCount|1000-50000': 1,
          creator: {
            id: '@integer(1, 1000)',
            nickname: '@cname',
            avatar: "@image('80x80', '@color', '#FFF', 'User')",
            'isVip|1': true
          },
          tags: Mock.mock({
            'list|1-4': ['流行', '摇滚', '民谣', '电子', '古典', '爵士', 'R&B', '说唱', '轻音乐', '纯音乐']
          }).list,
          createTime: '@datetime("yyyy-MM-dd HH:mm:ss")',
          updateTime: '@datetime("yyyy-MM-dd HH:mm:ss")',
          'tracks|20-50': [{
            'id|+1': 1,
            name: '@ctitle(2, 6)',
            artist: '@cname',
            album: '@ctitle(3, 8)',
            duration: '@integer(180, 300)',
            coverUrl: "@image('200x200', '@color', '#FFF', 'Song')",
            playUrl: 'https://music.example.com/play/@integer(1000, 9999).mp3',
            'playCount|1000-100000': 1,
            'order|+1': 1
          }]
        })
      }
    }
  },

  // 创建播放列表
  {
    url: '/api/playlist/create',
    method: 'POST',
    response: (options: any) => {
      const { name, description, isPrivate } = JSON.parse(options.body || '{}')
      return {
        code: 200,
        msg: '创建成功',
        data: Mock.mock({
          'id|+1': 1000,
          name: name || '@ctitle(4, 10)',
          description: description || '@csentence(10, 30)',
          coverUrl: "@image('400x400', '@color', '#FFF', 'New Playlist')",
          playCount: 0,
          trackCount: 0,
          subscriberCount: 1,
          isPrivate: isPrivate || false,
          creator: {
            id: 1,
            nickname: '测试用户',
            avatar: "@image('80x80', '@color', '#FFF', 'User')"
          },
          createTime: '@now("yyyy-MM-dd HH:mm:ss")',
          tracks: []
        })
      }
    }
  },

  // 添加歌曲到播放列表
  {
    url: '/api/playlist/add-track',
    method: 'POST',
    response: (options: any) => {
      const { playlistId, trackId } = JSON.parse(options.body || '{}')
      return {
        code: 200,
        msg: '添加成功',
        data: {
          playlistId,
          trackId,
          addTime: Mock.Random.datetime('yyyy-MM-dd HH:mm:ss')
        }
      }
    }
  },

  // 从播放列表移除歌曲
  {
    url: '/api/playlist/remove-track',
    method: 'DELETE',
    response: () => {
      return {
        code: 200,
        msg: '移除成功',
        data: null
      }
    }
  },

  // 收藏/取消收藏播放列表
  {
    url: '/api/playlist/subscribe',
    method: 'POST',
    response: (options: any) => {
      const { playlistId, action } = JSON.parse(options.body || '{}')
      return {
        code: 200,
        msg: action === 'subscribe' ? '收藏成功' : '取消收藏成功',
        data: {
          playlistId,
          isSubscribed: action === 'subscribe',
          subscribeTime: action === 'subscribe' ? Mock.Random.datetime('yyyy-MM-dd HH:mm:ss') : null
        }
      }
    }
  }
]

export default playlistMocks